//
//  PhotoView.m
//  Discover
//
//  Created by Angel on 15/11/10.
//  Copyright © 2015年 lq. All rights reserved.
//

#import "PhotoView.h"
#import "UIImageView+WebCache.h"

@implementation PhotoView

- (void)setImagearray:(NSArray *)imagearray
{
    _imagearray = imagearray;
    
}

+ (float)heightOfPhoto:(NSArray *)imageArray withInWidth:(float)width
{
    NSInteger count = imageArray.count;
    //根据图片的个数确定图片的宽度
    float imageWH = 0;
    float imageinterval = 5.0;//图片间间隔为5
    //一张图
    if (count == 1) {
        return 180;
    }
    //超过四张图,一行三张
    else if (count >4){
        imageWH = (width - imageinterval * 2) / 3;
        return imageWH;
    }
    //2-4张图，一行两张
    else{
        imageWH = (width - imageinterval * 2 ) / 2;
        
        return imageWH;
    }
}
+ (float)heightOfContent:(NSArray *)imageArray withInWidth:(float)width
{
    NSInteger count = imageArray.count;
    
    //一张图片
    if (count == 1){
        return 180;
    }
    else{
        return [PhotoView heightOfPhoto:imageArray withInWidth:width]*2+5;
    }
    
}
- (void)setImageArray:(NSArray *)imageArray withInWidth:(float)width
{
    for (UIView * view in [self subviews]) {
        [view removeFromSuperview];
    }
    
    NSInteger count = imageArray.count;
    if (count == 0) {
        return;
    }
    float x = 0;
    float y = 0;
    float imageinterval = 5.0;//图片间间隔为5
    float imageViewWH = [PhotoView heightOfPhoto:imageArray withInWidth:width];//图片的宽高
    //一张图片
    if (count == 1) {
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(x, y, imageViewWH, imageViewWH);
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[0]]];
        [self addSubview:imageView];
        
//        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImageClicked:)];
//        [imageView addGestureRecognizer:tap];
//        imageView.tag = 0;
//        imageView.userInteractionEnabled = YES;
    }
    //5-6张
    else if (count>4){
        for (NSInteger i = 0; i < count; i ++) {
            UIImageView * imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(x, y , imageViewWH, imageViewWH);
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]]];
//            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImageClicked:)];
//            [imageView addGestureRecognizer:tap];
//            imageView.tag = i;
            
            [self addSubview:imageView];
            
            (i+1)%3 == 0 ? (x =0,y+=imageViewWH+imageinterval) : (x += imageViewWH + imageinterval);
            
            imageView.userInteractionEnabled = YES;
        }
    }
    //2-4张
    else{
        for (NSInteger i = 0; i < count; i ++) {
            UIImageView * imageView = [[UIImageView alloc] init];
            imageView.frame = CGRectMake(x, y , imageViewWH, imageViewWH);
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageArray[i]]];
//            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bigImageClicked:)];
//            [imageView addGestureRecognizer:tap];
//            imageView.tag = i;
            
            [self addSubview:imageView];
            
            (i+1)%2 == 0 ? (x =0,y+=imageViewWH+imageinterval) : (x += imageViewWH + imageinterval);
            
            imageView.userInteractionEnabled = YES;
        }
        
    }
    
    
}


@end
